package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.model.UIDrawerMenu;

import java.util.ArrayList;

public class DrawerMenuAdapter extends RecyclerView.Adapter<DrawerMenuViewHolder> implements DrawerMenuViewHolder.OnClickDrawerMenuListener {

    public interface OnClickDrawerMenuListener {
        void onClickDrawerMenu(int position);
    }

    private ArrayList<UIDrawerMenu> mMenuList;

    private OnClickDrawerMenuListener mOnClickDrawerMenuListener;

    @Override
    public DrawerMenuViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_drawer_menu, viewGroup, false);
        DrawerMenuViewHolder viewHolder = new DrawerMenuViewHolder(view);
        viewHolder.setOnClickDrawerMenuListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DrawerMenuViewHolder drawerMenuViewHolder, int i) {
        drawerMenuViewHolder.setTitle(mMenuList.get(i).getTitle());
        drawerMenuViewHolder.setNumber("" + mMenuList.get(i).getNumber());
    }

    @Override
    public int getItemCount() {
        if (mMenuList != null && mMenuList.size() > 0) {
            return mMenuList.size();
        }
        return 0;
    }

    @Override
    public void onClickDrawerMenu(DrawerMenuViewHolder holder) {
        if (mOnClickDrawerMenuListener == null) {
            return;
        }
        mOnClickDrawerMenuListener.onClickDrawerMenu(holder.getLayoutPosition());
    }

    public void setMenuList(ArrayList<UIDrawerMenu> list) {
        mMenuList = list;
    }

    public void setOnClickDrawerMenuListener(OnClickDrawerMenuListener onClickDrawerMenuListener) {
        mOnClickDrawerMenuListener = onClickDrawerMenuListener;
    }
}
